package com.hnkywl.sts.service.zjgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.htgl.CghtDao;
import com.hnkywl.sts.dao.organ.EjzhDao;
import com.hnkywl.sts.dao.sfgl.SkshDao;
import com.hnkywl.sts.dao.zjgl.BzjDao;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.zjgl.Bzj;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.CalcUtils;
import com.hnkywl.sts.util.DateUtil;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.sfgl
 * 类描述:   付款审核服务类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class BzjService {
    private BzjDao bzjDao;
    private EjzhDao ejzhDao;
    private SkshDao skshDao;
    private CghtDao cghtDao;

    @Transactional
    public String findAllBzj(Long gsId, String khMc, String hth, Date createDate, int page, int rows) {
        Page<Bzj> bzjPage = this.bzjDao.findBzjList(gsId, khMc, hth, createDate, page, rows);
        List<Bzj> bzjList = bzjPage.getResult() == null ? new ArrayList<Bzj>() : bzjPage.getResult();
        String bzjJson = BaseUtils.fetchCollectionToJson(bzjList, "id", "nbgs.jc", "tx", "bzjlx.z", "startDate", "endDate", "khfksj", "mxts", "txl", "cght.htbm", "kh.jc", "ejzh.jc", "je", "sk.bm", "fk.bm", "status", "bzjbl", "ye");
        return "{\"total\":" + bzjPage.getTotalCount() + ",\"rows\":[" + bzjJson + "]}";
    }

    @Transactional
    public String bzjqueryByHtid(Long id) {
        List<Bzj> bzjList = this.bzjDao.findBzjblByhtid(id);
        Gson gson = new Gson();
        String result = gson.toJson(bzjList);
        return result;
    }

    @Transactional
    public Bzj findBzjDetail(Long id) {
        return bzjDao.get(id);
    }

    @Transactional
    public Bzj findBzjByCghtId(Long CghtId) {
        return bzjDao.findBzjByCghtId(CghtId);
    }

    public List<Bzj> findBzjByCght(Cght cght) {
        return this.bzjDao.findByProperty("cght", cght, null);
    }


    @Transactional
    public void saveBzj(Bzj bzj, boolean bool) {
        bzj.setDateCreated(new Date());
        bzj.setCreateUser(YgService.getCurrentYg());
        bzj.setCreateOrgan(YgService.getCurrentYg().getZggs());
        bzj.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        bzj.setLastUpdated(bzj.getDateCreated());
        //  bzj.setEjzh(ejzhDao.get(bzj.getEjzh().getId()));
        bzj.setBzjbl(bzj.getBzjbl());
        Sfk sk = this.skshDao.get(bzj.getSk().getId());
//        bzj.setJe(sk.getYe());
//        bzj.setYe(sk.getYe());
        bzj.setBzjlx(sk.getFkfs());
        // 没有结算的贴息
//        if (StringUtils.isNotBlank(sk.getSfjstx()) && sk.getSfjstx().equals("0")) {
//            bzj.setTx(sk.getTx());
//        }

        //收款方式为承兑时，合同关联保证金扣除贴息
        if (sk.getFkfs().getZ().equals("承兑")) {
            Cght cght = this.cghtDao.get(bzj.getCght().getId());
            long calDays;
            long reduceDays = DateUtil.getDay(sk.getEndDate(), sk.getKhfksj());
            if(cght.getMxts()==null){
                throw new RuntimeException("采购合同信息中免息天数不能为空请修改添加！");
            }else{
                calDays = reduceDays - cght.getMxts();
            }
            Double tx = 0.0;
            if (calDays > 0) {
                tx = CalcUtils.round(((sk.getYe() * cght.getTxl() / 100) / 30.0) * calDays, 2);
            }
            bzj.setTx(tx);
            bzj.setTxl(sk.getTxl());
            bzj.setJe(sk.getYe() - tx);
            bzj.setYe(sk.getYe() - tx);
            bzj.setMxts(cght.getMxts());
            bzj.setStartDate(sk.getStartDate());
            bzj.setEndDate(sk.getEndDate());
            bzj.setKhfksj(sk.getKhfksj());
            bzj.setJsts(calDays);
        }
        //否则，直接关联收款金额
        else{
            bzj.setJe(sk.getYe());
            bzj.setYe(sk.getYe());
            if (StringUtils.isNotBlank(sk.getSfjstx()) && sk.getSfjstx().equals("0")) {
            bzj.setTx(sk.getTx());
        }
        }

        bzj.setSfbcbzj(bool);
        this.bzjDao.save(bzj);
    }

    /**
     * 保证金的同一个合同和下游只可以新增一条保证金
     *
     * @return
     */
    public boolean checkHtAndXyByBzj(Long htId, Long xyId) {
        List<Bzj> bzjList = this.bzjDao.checkHtAndXyByBzj(htId, xyId);
        if (bzjList != null && bzjList.size() > 0) {
            return false;
        } else {
            return true;
        }
    }

    @Transactional
    public void updateBzj(Bzj bzj) {
        Bzj clone = this.findBzjDetail(bzj.getId());
        bzj.setSfbcbzj(clone.getSfbcbzj());
        clone = (Bzj) ReflectionUtils.getInstance(clone, bzj);
        //  clone.setEjzh(ejzhDao.get(bzj.getEjzh().getId()));
        clone.setLastUpdated(new Date());
        Sfk sk = this.skshDao.get(bzj.getSk().getId());
        clone.setJe(sk.getSfje());
        clone.setYe(sk.getYe());
        clone.setBzjlx(sk.getFkfs());
        this.bzjDao.save(clone);

    }

    @Transactional
    public void deleteBzj(Long id) {
        Bzj b = this.findBzjDetail(id);
        b.setLastUpdated(new Date());
        b.setDeleted(1);
        this.bzjDao.save(b);
    }

    /**
     * 解锁
     *
     * @param id
     */
    @Transactional
    public void hzBzj(Long id) {
        Bzj b = this.findBzjDetail(id);
        List<Bzj> bzjs = bzjDao.findBzjListByCghtId(b.getCght().getId());
        Cght cght = cghtDao.get(b.getCght().getId());

        if (bzjs != null && bzjs.size() > 0) {
            for (Bzj bzj : bzjs) {
                Sfk sk = this.skshDao.get(bzj.getSk().getId());
                bzj.setLastUpdated(new Date());
                bzj.setStatus(2);
                // 按比例释放
                if (bzj.getCght().getBzjfs() == 1 || StringUtils.equals("xsms", cght.getHtms())) {
                    sk.setYe(bzj.getYe());
                    sk.setZt("ytz");
                } else {
                    sk.setZt("yjs");
                }
                sk.setTxye(0.0);
                sk.setSfJsBzj(null);
                skshDao.save(sk);
                bzjDao.save(bzj);
            }
            cght.setLastsh(1);//进入最后一次赎货阶段
            cghtDao.save(cght);
        }

    }

    /**
     * 锁定
     *
     * @param id
     */
    @Transactional
    public void qrBzj(Long id) {
        Bzj bzj = this.findBzjDetail(id);
        bzj.setStatus(1);
        bzj.setLastUpdated(new Date());

        //锁定保证金1、将收款状态变成已锁定
        Sfk sk = this.skshDao.get(bzj.getSk().getId());

//        //收款方式为承兑时，合同关联保证金,收款余额扣除贴息
//        if (sk.getFkfs().getZ().equals("承兑")) {
//            Cght cght = this.cghtDao.get(bzj.getCght().getId());
//            long calDays;
//            long reduceDays = DateUtil.getDay(sk.getEndDate(), sk.getKhfksj());
//            if(cght.getMxts()==null){
//                throw new RuntimeException("采购合同信息中免息天数不能为空请修改添加！");
//            }else{
//                calDays = reduceDays - cght.getMxts();
//            }
//            Double tx = 0.0;
//            if (calDays > 0) {
//                tx = CalcUtils.round(((sk.getYe() * cght.getTxl() / 100) / 30.0) * calDays, 2);
//            }
//            sk.setTx(tx);
//            sk.setYe(sk.getYe() - tx);
//            sk.setMxts(cght.getMxts());
//            sk.setSfjstx("1");
//            sk.setJstx(calDays);
//            sk.setZt("ysd");
//            sk.setSfJsBzj(1);
//            sk.setLastUpdated(new Date());
//            bzjDao.save(bzj);
//            skshDao.save(sk);
//        }
//        else{
            sk.setSfJsBzj(1);
            sk.setZt("ysd");
            sk.setLastUpdated(new Date());
            bzjDao.save(bzj);
            skshDao.save(sk);
//        }

    }

    @Autowired
    public void setSkshDao(SkshDao skshDao) {
        this.skshDao = skshDao;
    }

    @Autowired
    public void setBzjDao(BzjDao bzjDao) {
        this.bzjDao = bzjDao;
    }

    @Autowired
    public void setEjzhDao(EjzhDao ejzhDao) {
        this.ejzhDao = ejzhDao;
    }

    @Autowired
    public void setCghtDao(CghtDao cghtDao) {
        this.cghtDao = cghtDao;
    }
}
